import random
import base64
import datetime
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import serialization, hashes


_KEY = b'''-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAzXRgJ3WBExLD7WPEZrDX
r3mBPAaTTxQ0dqu78+btdF25yPwt7OuotXo0+skG7vPdUiEEcC2eKjiGkby2JYp9
/WGQCHBUkAXeunowIRD3TYI+M82goKZgesPKKwrrlRQ9Y8CIvJtVtKXNpiEYhowk
xOJPe9MdUvswkSseOvOovNatoHlgKoBGyaKFZamZtxvT10X+wq6ACZ7Orq1EC1P0
fG8T/mcxvcbLXS5XU7CH2CrRuOl/JRpjm+piUkIb2qgoj9zEfHlOqDYfARaarOyt
AHQYa8vAJb+oGFGzlTqOPeiafCkEO2hD9EVh9MHpLrTiIe94vQP7C/dcNZsjZu7K
eIf/eRZCSLMtC9VS5A3QCY7YQ6sjkQWuWELNEJbnLTJg5xnzIXHyL7vocHkQgRi7
kVefCHpLWg2QE+mmocYRiq6G9vLr1uFHtJD4ddbZBGj3qSD6sKlulvdEUkw3vJaJ
1AzcIH1wEIymW5mSWV9LJLHAfgRYwrTfyDzSCip7Eto5JmrMtJM4BNxlfuMonsu9
HNOV2VEQpF4Xcor3Z+Ucd+EKwCvZl32558/TpVQ0Hx17gUrExhmWUsrdUrnXYD1v
6SWFCR+HFFyjEljGEWD4xOSNTof+Zw+YpMM3LXYvoHO0jmfvwsYQycTvX6PryL+z
oeSPFQhVJjqHWXvk1h799mkCAwEAAQ==
-----END PUBLIC KEY-----
'''
_PKEY = serialization.load_pem_public_key(_KEY)

data = _PKEY.encrypt(
    b'lol kek aabb',
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)


def gen(serial_number: str) -> str:
    seed = random.randint(0, 1_000_000)
    rnd = random.Random(seed)

    count = rnd.randint(7, 33)
    pos = rnd.randint(0, count-1)
    epos = rnd.randint(0, 1_000_000)
    epos = epos - (epos % count) + pos

    data = [
        str(rnd.randint(0, 1_000_000))
        for _ in range(count)
    ]
    data[pos] = str(seed)
    data.append(str(epos))
    data.append(serial_number)
    data.append(str(datetime.datetime.now(datetime.timezone.utc).timestamp()))

    key = '\n'.join(data).encode('utf-8')
    key = _PKEY.encrypt(
        key,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    return base64.b64encode(key).decode('ascii')
